package com.pesilo.mvc.view.component.show.watch
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;
	import com.peislo.watch.ui.RulerItemPanel;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.event.PesiloEvent;
	import com.pesilo.mvc.locator.ModelLocator;
	import com.pesilo.util.UIUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class RulerItemComponent extends BaseComponent
	{
		private var index:int;
		private var rulerPanel:RulerItemPanel;
		
		private var movePanel:Sprite;
		private var imgPanel:Sprite;
		private var sign:Sprite;
		
		private var bmd:BitmapData;
		private var bmp:Bitmap;
		public function RulerItemComponent(index:int,bmd:BitmapData)
		{
			this.index=index;
			this.bmd=bmd;
			super();
		}
		
		override public function initUI():void{
			rulerPanel=new RulerItemPanel();
			this.addChild(rulerPanel);
			
			movePanel=UIUtil.getElement(rulerPanel,"movePanel")as Sprite;
			imgPanel=UIUtil.getElement(rulerPanel,"imgPanel")as Sprite;
			sign=UIUtil.getElement(rulerPanel,"sign")as Sprite;
			sign.visible=false;
			
			bmp=new Bitmap(bmd,"auto",true);
			imgPanel.addChild(bmp);
		}

		override public function initEvent(b:Boolean=true):void{
			if(b){
				rulerPanel.addEventListener(MouseEvent.CLICK,__mouseClickHandle);
				rulerPanel.addEventListener(MouseEvent.ROLL_OVER,__mouseRollHandle);
				rulerPanel.addEventListener(MouseEvent.ROLL_OUT,__mouseRollHandle);
			}else{
				rulerPanel.removeEventListener(MouseEvent.CLICK,__mouseClickHandle);
				rulerPanel.removeEventListener(MouseEvent.ROLL_OVER,__mouseRollHandle);
				rulerPanel.removeEventListener(MouseEvent.ROLL_OUT,__mouseRollHandle);
			}
		}
		
		protected function __mouseClickHandle(e:MouseEvent):void
		{
			showSign(true);
			dispatchEvent(new PesiloEvent(PesiloEvent.ITEM_CLICK_EVENT,index));
		}
		
		protected function __mouseRollHandle(e:MouseEvent):void
		{
			switch(e.type){
				case MouseEvent.ROLL_OVER:
					dispatchEvent(new PesiloEvent(PesiloEvent.ITEM_ROLL_OVER_EVENT,index));
					break;
				case MouseEvent.ROLL_OUT:
					dispatchEvent(new PesiloEvent(PesiloEvent.ITEM_ROLL_OUT_EVENT,index));
					break;
			}
		}
		
		/**
		 * 是否显示出指标
		 * 
		 */
		public function showSign(b:Boolean):void{
			if(b){
				if(ModelLocator.rulerCount["current"]){
					ModelLocator.rulerCount["current"].sign.visible=false;
				}
				ModelLocator.rulerCount["current"]=this;
			}
			sign.visible=b;
		}
		
		/**
		 * 图片位置滑动
		 */
		public function roll(t:int=0):void {
			switch(t) {
				case 0:
					TweenMax.to(imgPanel, 0.2, { y:0,ease:Quad.easeOut(1,2,3,4) } );
					TweenMax.to(sign, 0.2, { y:-30,ease:Quad.easeOut(1,2,3,4) } );
					break;
				case 1:
					TweenMax.to(imgPanel, 0.2, { y: -20,ease:Quad.easeOut(1,2,3,4) } );
					TweenMax.to(sign, 0.2, { y: -50,ease:Quad.easeOut(1,2,3,4) } );
					break;
				case 2:
					TweenMax.to(imgPanel, 0.2, { y: -35,ease:Quad.easeOut(1,2,3,4) } );
					TweenMax.to(sign, 0.2, { y: -60,ease:Quad.easeOut(1,2,3,4) } );
					break;
				case 3:
					TweenMax.to(imgPanel, 0.2, { y: -60,ease:Quad.easeOut(1,2,3,4) } );
					TweenMax.to(sign, 0.2, { y: -80,ease:Quad.easeOut(1,2,3,4) } );
					break;
			}
		}
		
		override public function initLocation():void{
			
		}
	}
}